<?php
$page_type = "A";
require_once 'header.php';

if (!empty($_GET["update_error"]))
  $msg = "<font color=\"#CC0000\">{$_GET["update_error"]}</font>";
else
  $msg = '';
  
// Verify where we are from, client.php or client_update_response.php
if (!isset($_GET["update_error"])) { // From client.php
  // Fetch the record to be updated
  $input_client_id = $_GET["client_id"];

  $sql = "select * from CLIENT where Client_ID='$input_client_id'";

  $result_array = execute_sql_in_oracle($sql);
  $result = $result_array["flag"];
  $cursor = $result_array["cursor"];

  if (!$result){
    display_oracle_error_message($cursor);
    die("Query using inputted client ID failed.");
  }

  // Get client information from database query result
  $values = oci_fetch_array($cursor);
  oci_free_statement($cursor);

  $client_id = $values[0];
  $password = $values[1];
  $fname = $values[2];
  $lname = $values[3];
  $aflag = $values[4];
  $sflag = $values[5];
  $sid = $values[6];
  $dob = $values[7];
  $street = $values[8];
  $city = $values[9];
  $state = $values[10];
  $zip = $values[11];
  $stflag = $values[12];
  $pflag = $values[13];
  
  $client_type = "";
  
  // Client type
  if ($aflag == '1')
    $client_type = "A";
  if ($sflag == '1') 
    $client_type = "S";
  if ($aflag == '1' and $sflag == '1')
    $client_type = "B";
	
  // Student type
  if ($stflag == "0")
    $student_type = "U";
  else if ($stflag == "1")
    $student_type = "G";
  else
    $student_type = "";
	
  // Probation status
  if ($pflag == "0")
    $probation = "n";
  else if ($pflag == "1")
    $probation = "y";
  else
    $probation = "";
}
else { // From client_update_response.php
  // Initialize field variables
  $client_id = "";
  $password = "";
  $client_type = "";
  $fname = "";
  $lname = "";
  $dob = "";
  $sid = "";
  $street = ""; 
  $city = "";
  $state = "";
  $zip = "";
  $student_type = "";
  $probation = "";
  
  // Obtain values of the record to be updated directly
  if (isset($_SESSION['client_id']))
    $client_id = $_SESSION['client_id'];
  if (isset($_SESSION['password']))
    $password = $_SESSION['password'];
  if (isset($_SESSION['client_type']))
    $client_type = $_SESSION['client_type'];
  if (isset($_SESSION['fname']))
    $fname = $_SESSION['fname'];
  if (isset($_SESSION['lname']))
    $lname = $_SESSION['lname'];
  if (isset($_SESSION['dob']))
    $dob = $_SESSION['dob'];
  if (isset($_SESSION['sid']))
    $sid = $_SESSION['sid'];
  if (isset($_SESSION['street']))
    $street = $_SESSION['street']; 
  if (isset($_SESSION['city']))
    $city = $_SESSION['city'];
  if (isset($_SESSION['state']))
    $state = $_SESSION['state'];
  if (isset($_SESSION['zip']))
    $zip = $_SESSION['zip'];
  if (isset($_SESSION['student_type']))
    $student_type = $_SESSION['student_type'];
  if (isset($_SESSION['input_pflag']))
    $probation = $_SESSION['input_pflag'];
}

echo "<h2>User Record Update Form</h2>";

// Display the record to be updated
echo("
  <form method=\"post\" action=\"client_update_response.php\">
  Client ID (Read-only): <input type=\"text\" readonly value = \"$client_id\" size=\"20\" maxlength=\"12\" name=\"client_id\"> <br> 
  Current Password (Required): <input type=\"password\" value = \"$password\" size=\"20\" maxlength=\"12\" name=\"password\">  <br>
  First Name (Required): <input type=\"text\" value = \"$fname\" size=\"20\" maxlength=\"30\" name=\"fname\">  <br>
  Last Name (Required): <input type=\"text\" value = \"$lname\" size=\"20\" maxlength=\"30\" name=\"lname\">  <br>
  SID: <input type=\"text\" value =\"$sid\" size=\"20\" maxlength=\"10\" name=\"sid\"><br> 
  DOB(DD-MON-YY): <input type=\"text\" value =\"$dob\" size=\"20\" maxlength=\"9\" name=\"dob\"><br> 
  Street: <input type=\"text\" value =\"$street\" size=\"20\" maxlength=\"40\" name=\"street\"><br>
  City: <input type=\"text\" value =\"$city\" size=\"20\" maxlength=\"32\" name=\"city\"><br>
  State: <input type=\"text\" value =\"$state\" size=\"20\" maxlength=\"32\" name=\"state\"><br>
  Zip: <input type=\"text\" value =\"$zip\" size=\"20\" maxlength=\"12\" name=\"zip\"><br>");

// Reselect client type dropdownlist after postback
if ($client_type=="") {
  $choose_one_select = htmlspecialchars("selected=\"selected\"");
}
if ($client_type=="A") {
  $a_selection = htmlspecialchars("selected=\"selected\"");
}
if ($client_type=="B") {
  $b_selection = htmlspecialchars("selected=\"selected\"");
}
if ($client_type=="S") {
  $s_selection = htmlspecialchars("selected=\"selected\"");
}

// Create the dropdown list for the client type
echo("Client Type:
      <select name=\"client_type\">
      <option value=\"\" $choose_one_select>Choose One</option>
      <option value=\"A\" $a_selection>Administrator</option>
      <option value=\"B\" $b_selection>Student Administrator</option>
      <option value=\"S\" $s_selection>Student</option>	
      </select>");
  
// Reselect student type dropdownlist after postback
if ($student_type=="") 
  $choose_one_select1 = htmlspecialchars("selected");
else if ($student_type=="U") 
  $u_selection = htmlspecialchars("selected");
else if ($student_type=="G") 
  $g_selection = htmlspecialchars("selected");
  
// Create the dropdown list for the student type
echo("
  Student Type:
  <select name=\"student_type\">
  <option value=\"\" $choose_one_select1>Choose One</option>
  <option value=\"U\" $u_selection>Undergraduate</option>
  <option value=\"G\" $g_selection>Graduate</option></select>");
  
// Reselect probation status dropdownlist after postback
if ($probation=="") 
  $choose_one_select2 = htmlspecialchars("selected");
else if ($probation=="y") 
  $yp_selection = htmlspecialchars("selected");
else if ($probation=="n") 
  $np_selection = htmlspecialchars("selected");
  
// Create the dropdown list for the probation status
echo("
  Probation Status:
  <select name=\"probation\">
  <option value=\"\" $choose_one_select2>Choose One</option>
  <option value=\"y\" $yp_selection>Yes</option>
  <option value=\"n\" $np_selection>No</option>
  ");

echo("
  </select>  <input type=\"submit\" value=\"Update\">
  <input type=\"reset\" value=\"Reset\">
  </form>

  <form method=\"post\" action=\"client.php\">
  <input type=\"submit\" value=\"Go Back\">
  </form>$msg");
?>